package com.open.free.boot.core.xss;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;


import java.io.IOException;

/**
 * 处理JSON参数数据
 */
public class XssStringJsonSerializer extends JsonSerializer<String> {
    @Override
    public Class<String> handledType() {
        return String.class;
    }

    @Override
    public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(value!=null){
            String encodeValue = Jsoup.clean(value, Whitelist.relaxed());
            jsonGenerator.writeString(encodeValue);
        }
    }
}
